<!-- 修改密码 -->
<template>
  <client-only>
    <el-dialog
      v-model="visible"
      width="560px"
      :z-index="1000"
      draggable
      @close="resetForm"
      :show-close="false"
    >
      <template #header="{ close, titleId, titleClass }">
        <div class="header">
          <div :id="titleId" :class="titleClass">修改密码</div>
          <div class="close">
            <el-icon @click="close">
              <CloseBold />
            </el-icon>
          </div>
        </div>
      </template>
      <div class="pb-16px">
        <div>
          <el-form
            ref="formRef"
            label-position="top"
            :model="formData"
            :rules="rules"
          >
            <el-form-item label="手机号" prop="phone">
              <el-input
                v-model="formData.phone"
                placeholder="请输入手机号"
                disabled
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="原密码" prop="oldPwd">
              <el-input
                v-model="formData.oldPwd"
                placeholder="请输入原密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="pwd">
              <el-input
                v-model="formData.pwd"
                placeholder="请输入新密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPwd">
              <el-input
                v-model="formData.confirmPwd"
                placeholder="请再次输入密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button class="w-full" round type="primary" @click="onSubmit"
              >确定修改</el-button
            >
          </div>
        </div>
      </div>
    </el-dialog>
  </client-only>
</template>

<script setup>
import { encryptData } from "@/utils/encrypt";

const user = useStore.user();
const visible = ref(false);
const show = () => {
  visible.value = !visible.value;
};

const formData = reactive({
  phone: user.userInfo.phone,
  oldPwd: "",
  pwd: "",
  confirmPwd: "",
});
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== formData.pwd) {
    callback(new Error("两次密码不一致！"));
  } else {
    callback();
  }
};
const rules = {
  phone: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
  oldPwd: [{ required: true, message: "请输入原", trigger: "blur" }],
  pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
  confirmPwd: [{ required: true, validator: validatePass2, trigger: "blur" }],
};

const formRef = ref();
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      api
        .post(
          "/user/front/updatePwd",
          {
            phone: formData.phone,
            oldPwd: encryptData(formData.oldPwd),
            pwd: encryptData(formData.pwd),
            confirmPwd: encryptData(formData.confirmPwd),
          },
          { msg: true, loading: true }
        )
        .then((res) => {
          resetForm();
        });
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

const resetForm = () => {
  formRef.value.resetFields();
  visible.value = false;
};
defineExpose({ show });
</script>
<style lang="scss" scoped>
.header {
  text-align: center;
  position: relative;
  padding-top: 16px;
  padding-bottom: 16px;

  .el-dialog__title {
    font-weight: bold;
    font-size: 20px;
    line-height: 26px;
  }

  .close {
    position: absolute;
    width: 32px;
    height: 32px;
    display: flex;
    justify-content: center;
    align-items: center;
    right: -22px;
    top: -22px;
    background-color: #fff;
    border-radius: 32px;
    cursor: pointer;
  }
}

::v-deep(.center) {
  .captcha {
    flex: 1;
    margin-right: 10px;
  }

  .toggle {
    display: flex;
    justify-content: flex-end;
  }

  .mt40 {
    margin-top: 40px;
  }
}

.btns {
  > div {
    margin-top: 24px;
  }

  .fullBtn {
    width: 100%;
  }
}

::v-deep(.el-input) {
  line-height: 56px;

  .el-input__wrapper {
    border-radius: 28px;
  }

  .el-input__inner {
    --el-input-inner-height: calc(56px - 2px) !important;
  }
}
</style>
